package com.zhaozhaonews.wemedia.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author WanJl
 * @version 1.0
 * @title ThreadLocalTestController
 * @description
 * @create 2025/2/18
 */
@RestController
@RequestMapping("/threadLocal")
@Slf4j
public class ThreadLocalTestController {

    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();


    @GetMapping("/{s}")
    public String testRequest(@PathVariable String s){
        //向threadLocal本地线程添加一个变量值
        threadLocal.set(s);
        //获取变量值
        log.info("threadLocal is " + threadLocal.get());
        print();
        //获取变量值
        return threadLocal.get();
    }

    public static void print(){
        log.info("又一个方法打印线程变量...." + threadLocal.get());
        threadLocal.remove();
    }

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ThreadLocalTestController.threadLocal.set("张三");
                print();
                log.info("删除后打印本地线程...." + threadLocal.get());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                ThreadLocalTestController.threadLocal.set("李四");
                print();
                log.info("删除后打印本地线程...." + threadLocal.get());
            }
        }).start();
    }



}
